/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.admin.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.modules.admin.entity.MaintainByScoreQuestion;
import org.springblade.modules.admin.entity.MaintainMsg;
import org.springblade.modules.admin.entity.MaintainScoreMsg;
import org.springblade.modules.admin.enums.MsgTypeEnum;
import org.springblade.modules.admin.mapper.MaintainScoreMsgMapper;
import org.springblade.modules.admin.service.IMaintainScoreMsgService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.modules.admin.service.MaintainMsgService;
import org.springblade.modules.admin.vo.BuildingAlreadyVO;
import org.springblade.modules.admin.vo.StoreUserRoleVO;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 评分消息定时器设置 服务实现类
 *
 * @author ysq
 * @since 2025-06-25
 */
@Service
public class MaintainScoreMsgServiceImpl extends BaseServiceImpl<MaintainScoreMsgMapper, MaintainScoreMsg> implements IMaintainScoreMsgService {

	@Autowired
	private MaintainMsgService maintainMsgService;

	@Autowired
	private IUserService userService;


	@Override
	public void sendByStoreNoPass() {
		List<MaintainByScoreQuestion> list = baseMapper.getByStoreNoPassList(DateUtil.beginOfDay(DateUtil.yesterday()), DateUtil.endOfDay(DateUtil.yesterday()));
		if (list.size() == 0) {
			return;
		}
		List<String> tenantIds = list.stream().map(MaintainByScoreQuestion::getTenantId).distinct().collect(Collectors.toList());
		for (String tenantId : tenantIds) {
			List<Long> userIds = new ArrayList<>();
			MaintainScoreMsg maintainScoreMsg = this.getOne(Wrappers.<MaintainScoreMsg>lambdaQuery().eq(MaintainScoreMsg::getTenantId, tenantId).eq(MaintainScoreMsg::getStatus, 1));
			maintainScoreMsg = JSON.parseObject(JSON.toJSONString(maintainScoreMsg), MaintainScoreMsg.class);
			if (maintainScoreMsg != null && maintainScoreMsg.getIsOpen()) {
				List<StoreUserRoleVO> userInfo = maintainScoreMsg.getUserInfo();
				if (userInfo != null && userInfo.size() > 0) {
					userIds = userInfo.stream().map(StoreUserRoleVO::getUserId).collect(Collectors.toList());
				} else {
					List<Long> roleIds = maintainScoreMsg.getRoleInfo().stream().map(StoreUserRoleVO::getRoleId).collect(Collectors.toList());
					userIds = userService.selectUserByRole(roleIds, tenantId).stream().map(User::getId).collect(Collectors.toList());
				}
			}
			if (userIds.size() > 0) {
				List<MaintainByScoreQuestion> byScoreQuestions = list.stream().filter(o -> o.getTenantId().equals(tenantId)).collect(Collectors.toList());
				List<Long> buiIds = byScoreQuestions.stream().map(MaintainByScoreQuestion::getBuildingId).distinct().collect(Collectors.toList());
				MaintainMsg maintainMsg = new MaintainMsg();
				maintainMsg.setTenantId(tenantId);
				maintainMsg.setTitle("有" + buiIds.size() + "个项目" + byScoreQuestions.size() + "台梯保养作业不合格");
				maintainMsg.setContent("保养作业不合规消息");
				maintainMsg.setType(MsgTypeEnum.BY_SCORE_NO_PASS.getId());
				maintainMsgService.sendMsg(maintainMsg, userIds, tenantId);
			}
		}
	}

	@Override
	public void abarbeitungPlanTimeOut() {
		List<MaintainByScoreQuestion> list = baseMapper.getAbarbeitungPlanTimeOut();
		if (list.size() == 0) {
			return;
		}
		List<String> tenantIds = list.stream().map(MaintainByScoreQuestion::getTenantId).distinct().collect(Collectors.toList());
		for (String tenantId : tenantIds) {
			List<Long> userIds = new ArrayList<>();
			MaintainScoreMsg maintainScoreMsg = this.getOne(Wrappers.<MaintainScoreMsg>lambdaQuery().eq(MaintainScoreMsg::getTenantId, tenantId).eq(MaintainScoreMsg::getStatus, 2));
			maintainScoreMsg = JSON.parseObject(JSON.toJSONString(maintainScoreMsg), MaintainScoreMsg.class);
			if (maintainScoreMsg != null && maintainScoreMsg.getIsOpen()) {
				List<StoreUserRoleVO> userInfo = maintainScoreMsg.getUserInfo();
				if (userInfo != null && userInfo.size() > 0) {
					userIds = userInfo.stream().map(StoreUserRoleVO::getUserId).collect(Collectors.toList());
				} else {
					List<Long> roleIds = maintainScoreMsg.getRoleInfo().stream().map(StoreUserRoleVO::getRoleId).collect(Collectors.toList());
					userIds = userService.selectUserByRole(roleIds, tenantId).stream().map(User::getId).collect(Collectors.toList());
				}
			}
			if (userIds.size() > 0) {
				List<MaintainByScoreQuestion> byScoreQuestions = list.stream().filter(o -> o.getTenantId().equals(tenantId)).collect(Collectors.toList());
				List<Long> buiIds = byScoreQuestions.stream().map(MaintainByScoreQuestion::getBuildingId).distinct().collect(Collectors.toList());
				MaintainMsg maintainMsg = new MaintainMsg();
				maintainMsg.setTenantId(tenantId);
				maintainMsg.setTitle("有" + buiIds.size() + "个项目" + byScoreQuestions.size() + "台梯整改逾期未完成");
				maintainMsg.setContent("整改工单逾期未完成");
				maintainMsg.setType(MsgTypeEnum.ZG_TIME_OUT.getId());
				maintainMsgService.sendMsg(maintainMsg, userIds, tenantId);
			}
		}
	}
}
